package com.huilai.platform.country_platform.controller;

import com.huilai.platform.country_platform.model.PartyActivities;
import com.huilai.platform.country_platform.model.VillageActivities;
import com.huilai.platform.country_platform.model.VillageCommittee;
import com.huilai.platform.country_platform.service.PartyService;
import com.huilai.platform.country_platform.service.VillageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

/**
 * 作者：洪霓
 * 日期：2023/3/6
 * 注释：
 */
@Controller
@RequestMapping("/party")
public class PartyController {

    @Autowired
    private VillageService villageService;

    @Autowired
    private PartyService partyService;

    @RequestMapping("/committeeUI")
    public String committeeUI(Model model, @ModelAttribute("searchName") String searchName) {
//        智慧党建-党务管理
        List<VillageActivities> topActivities1 = villageService.getTopActivities("办事指南");
        model.addAttribute("topActivities1", topActivities1);
        List<VillageActivities> topActivities2 = villageService.getTopActivities("政策法规");
        model.addAttribute("topActivities2", topActivities2);
        List<VillageCommittee> villageCommitteeList1 = villageService.committeeUI(searchName, "党政综合办公室");
        model.addAttribute("villageCommitteeList1", villageCommitteeList1);
        List<VillageCommittee> villageCommitteeList2 = villageService.committeeUI(searchName, "党建工作办公室");
        model.addAttribute("villageCommitteeList2", villageCommitteeList2);
        List<VillageCommittee> villageCommitteeList3 = villageService.committeeUI(searchName, "人大办公室");
        model.addAttribute("villageCommitteeList3", villageCommitteeList3);
        List<VillageCommittee> villageCommitteeList4 = villageService.committeeUI(searchName, "纪检监察办公室");
        model.addAttribute("villageCommitteeList4", villageCommitteeList4);
        model.addAttribute("activeTab", "党务管理");
        return "party/party-committee";
    }

    @RequestMapping("/activitiesUI")
    public String activitiesUI(Model model, @ModelAttribute("searchName")String searchName, @ModelAttribute("contentType")String contentType,  @PageableDefault(size = 10) Pageable pageable){
//        智慧党建-党员活动等
        List<VillageActivities> topActivities1 = villageService.getTopActivities("办事指南");
        model.addAttribute("topActivities1", topActivities1);
        List<VillageActivities> topActivities2 = villageService.getTopActivities("政策法规");
        model.addAttribute("topActivities2", topActivities2);

        Page<PartyActivities> page = partyService.activitiesUI(searchName, contentType, pageable);
        model.addAttribute("page", page);
        model.addAttribute("activeTab",contentType);
        return "party/party-activities";
    }

    @RequestMapping("/activitiesDetailsUI")
    public String activitiesDetailsUI(Model model, @ModelAttribute("id")Integer id, @PageableDefault(size = 10)Pageable pageable){
        List<VillageActivities> topActivities1 = villageService.getTopActivities("办事指南");
        model.addAttribute("topActivities1", topActivities1);
        List<VillageActivities> topActivities2 = villageService.getTopActivities("政策法规");
        model.addAttribute("topActivities2", topActivities2);
        PartyActivities partyActivities = partyService.findActivitiesyId(id);
        model.addAttribute("partyActivities",partyActivities);
        model.addAttribute("contentType", partyActivities.getContentType());
        return "party/party-activities-details";
    }

    @RequestMapping("/downloadFile")
    public String downloadFile(HttpServletResponse response, @ModelAttribute("filename")String filename) throws UnsupportedEncodingException {
        String rfile = "D:/ideaProject/country_platform/src/main/resources/static/files";
        File file = new File(rfile + File.separator + filename);
        if (file.exists()){
            response.setContentType("application/octet-stream;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Disposition", "attachment;fileName=" +   java.net.URLEncoder.encode(filename,"UTF-8"));
            byte[] buffer = new byte[1024];
            FileInputStream fis = null; //文件输入流
            BufferedInputStream bis = null;

            OutputStream os = null; //输出流
            try{
                os = response.getOutputStream();
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                int i = bis.read(buffer);
                while(i != -1){
                    os.write(buffer);
                    i = bis.read(buffer);
                }

            }catch (Exception e){
                e.printStackTrace();
            }
            System.out.println("----------file download---" + filename);
            try {
                bis.close();
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }
}
